--
-- @Author:      name
-- @DateTime:    2018-8-21 17:03:10
-- @Description: 消息的处理

local skynet = require "skynet"
local log = require "Log"
local CommandBase = require ".common.CommandBase"
require "common"

local AssertEx = AssertEx
local Command = class("Command",CommandBase)
---------------------------------------------------------
-- Private
---------------------------------------------------------
function Command:ctor(message,machine)
	self.super:ctor(message,machine)
	self.msg = message 
	self.machine = machine 
	self.close  = false
	self.tableid = self.machine:getTableId()
	self.gameid = getGameIdbyTable(self.tableid)
	self:register()
end

--注册本服务里的消息
function Command:register()
	self.msg:registerSelf('ps_status_req',handler(self,self.getStatus))
	self.msg:registerSelf('ps_race_bank_req',handler(self,self.raceBank))
	self.msg:registerSelf('ps_bet_req',handler(self,self.bet))
	self.msg:registerSelf('ps_open_card_req',handler(self,self.openCard))
	self.msg:registerSelf('ps_player_req',handler(self,self.getPlayerInfo))
	self.msg:registerSelf('enter_table_req',handler(self,self.enterTable))
	self.msg:registerSelf('leave_table_req',handler(self,self.leaveTable))
end

function Command:enterTable(playerid,playerInfo)
	local res = self.super.enterTable(self, playerid, playerInfo)	
	local sql = string.format("replace into user_online (uid, sid, game, `leave`) values(%d, %d, %d, %d);", playerid, self.gameid, self.gameid, 2)
	skynet.send("mysql_service", "lua", "execute", sql)	
	print("------------------>enterTable", self.machine:getState(), self.machine:getOperTime(), playerid)
	self.machine:enterTable(playerid)
	return res
end

function Command:leaveTable(playerid,playerInfo)
	local res = self.super.leaveTable(self, playerid, playerInfo)	
	local sql = string.format("delete from user_online where uid = %d;", playerid)
	skynet.send("mysql_service", "lua", "execute", sql)
	return res
end
---------------------------------------------------------
-- REQUEST
---------------------------------------------------------

-- 得到桌子状态
function Command:getStatus(playerid)	
	print("-------------------->getStatus", playerid)
	local player = self.machine:getPlayer(playerid)
	AssertEx(player, 'ps_status_res', 10)
	--重连进入
	player.disconnect = false
	player.ready = true
	local status = self.machine:getTableStatus(playerid)
	return {ps_status_res = status}
end

------------------------------------------------------------------------------------------------
---游戏里的操作
------------------------------------------------------------------------------------------------
--抢庄
function Command:raceBank(playerid, args)
	local player = self.machine:getPlayer(playerid)
	AssertEx(player, 'ps_race_bank_res', 10)	
	local res = self.machine:playerRaceBank(playerid, args)
	if not res then
		return
	end
	return {ps_race_bank_res = res}	
end

--下注
function Command:bet(playerid, args)
	--print("_________Command:bet", args,  'playerid:' ..playerid)
	local player = self.machine:getPlayer(playerid)
	AssertEx(player, 'ps_bet_res', 10)	
	local res = self.machine:onPlayerBet(playerid,args)
	if not res then
		return
	end
	return {ps_bet_res = res}	
end

function Command:openCard(playerid, args)
	local player = self.machine:getPlayer(playerid)
	AssertEx(player, 'ps_open_card_res', 10)	
	local res = self.machine:onOpenCard(playerid,args)
	if not res then
		return
	end
	return {ps_open_card_req = res}	
end

function Command:getPlayerInfo(playerid, args)
	local player = self.machine:getPlayer(playerid)
	AssertEx(player, 'ps_player_res', 10)	
	local resPlayer = self.machine:getPlayerInfo(playerid)
	return {ps_player_res = {player = resPlayer}}	
 end 



return Command